#pragma once
#include <string>
#include <fstream>
#include <vector>

#define CHL_BGM 1

using namespace std;

class NoteManager
{
	public:
	enum FILETYPE
	{
		FILE_IDM,
		FILE_OSU,
		FILE_BMS
	};

	FILETYPE type;
	ifstream file;
	string mapfilepath;
	string dataPath;
	string musicFile = "";
	string previewFile = "";
	string bgFile = "";
	int setId = 0;
	int mapId = 0;
	int previewPoint = 0;
	int gameMode = 3;
	int difficulty = 0;
	int level = 0;
	int trackCount = 0;
	int beatDivisor = 0;
	double bpmMax = 0;
	double bpmMin = 0;
	float hispeed = 1;
	int bmsLNType = 1;
	string bmsLNObj = "";
	struct MetaData
	{
		string title = "";
		string titleU = "";
		string artist = "";
		string artistU = "";
		string noter = "";
		string version = "";
		string source = "";
		string tag = "";
	};
	MetaData metaData;

	struct Timing
	{
		int startTime = 0;
		double mspb = 0;
		double bpm = 0;
		float speedModer = 1;
		int beatsPerBar = 0;
		bool isHighlight = false;
	};
	//Timing* timingSegs;
	vector<Timing> timingSegs;
	int timingSegsSize = 0;

	struct Note
	{
		int positionX = 0;
		int startTime = 0;
		enum NoteType
		{
			SINGLE,
			LONG
		}noteType = SINGLE;
		int endTime = 0;
		int bmsSoundIndex = -1;
		string osuSoundFile = "";
	};
	vector<Note>* trackNotes;

	struct WAVINFO
	{
		string filename = "";
		int seqno = -1;
	};
	WAVINFO* wavdefs;
	int wavseq = 0;
	string* bmpdefs;
	float* bpmdefs;
	struct BMSDATA
	{
		int mensure;
		int channel;
		string message;
	};
	vector<BMSDATA> bmsData;
	struct CHANNELBGMOBJECT
	{
		int wavseq = 0;
		float time = 0;
		string filename = "";
	};
	vector<CHANNELBGMOBJECT> chlBgm;

	public:
	NoteManager(string filepath, FILETYPE filetype = FILE_OSU);
	virtual ~NoteManager();
	static NoteManager* create(string filepath, FILETYPE filetype = FILE_OSU);

	void loadfile(string filepath, FILETYPE filetype);

	/*osu file format process*/
	void OsuLoadFile();
	void OsuLoadFileInfo(string& retBuffer);
	void OsuLoadFileTiming(string& retBuffer);
	void OsuFileLoadNote();
	int OsuGetMapLevel(vector<Note>& notes);
	void OsuLoadBGFilename(string& retBuffer);

	/*bms file format process*/
	int BMSGetNumber(string cnum);
	void BMSLoadFile();
	void BMSDataProcess();

	int getTotalNotes();
	int getTotalNotes(int& singlecount, int& longcount);
	string getPreviewFileName();
};